﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OF.Common;
using OF.Push;
namespace OF.BLL.Push
{
    public class BaiduPush
    {
        string BaiduApiKey = Utils.AppSettingByKey("BaiduApiKey");
        string BaiduSecretKey = Utils.AppSettingByKey("BaiduSecretKey");

        #region 推送消息接口

        /// <summary>
        /// 推送消息接口
        /// </summary>
        /// <param name="msg">消息内容</param>
        /// <param name="title">消息标题,可以为空</param>
        /// <param name="msg_type">消息类型，0：消息（透传给应用的消息体）1：通知（对应设备上的消息通知）默认值为0</param>
        /// <param name="device_type">设备类型，取值范围为：1～5云推送支持多种设备，各种设备的类型编号如下：1：浏览器设备；2：PC设备；3：Andriod设备；4：iOS设备；5：Windows Phone设备；如果存在此字段，则向指定的设备类型推送消息。 默认为android设备类型。</param>
        /// <param name="push_type">推送类型，取值范围为：1～3，1：单个人，必须指定user_id 和 channel_id （指定用户的指定设备）或者user_id（指定用户的所有设备），2：一群人，必须指定 tag，3：所有人，无需指定tag、user_id、channel_id</param>
        /// <param name="userid">用户标识，在Android里，channel_id + userid指定某一个特定client。不超过256字节，如果存在此字段，则只推送给此用户。</param>
        /// <param name="channelid">通道标识</param>
        /// <param name="tag">标签名称，不超过128字节</param>
        public string PushMsg(string msg, string title, int msg_type, uint device_type, int push_type, string userid, string channelid, string tag)
        {
            string response = "";
            try
            {
                OF.Push.BaiduPush Bpush = new OF.Push.BaiduPush("POST", BaiduSecretKey);
                String apiKey = BaiduApiKey;
                String messages = "";
                String method = "push_msg";
                TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
                uint unixTime = (uint)ts.TotalSeconds;

                uint message_type;
                string messageksy = "xxxxxx";
                if (msg_type == 0)
                {
                    message_type = 0;
                    messages = msg;
                }
                else
                {
                    message_type = 1;
                    if (device_type == 4)
                    {
                        device_type = 4;
                        IOSNotification notification = new IOSNotification();
                        notification.title = title;
                        notification.description = msg;
                        messages = notification.getJsonString();
                    }
                    else
                    {
                        BaiduPushNotification notification = new BaiduPushNotification();
                        notification.title = title;
                        notification.description = msg;
                        notification.notification_basic_style=0x07;
                        //notification.open_type = 2;
                        //notification.pkg_content = "intent:#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10000000;component=com.szkj.mobiletoken/.MainActivity;end";
                        messages = notification.getJsonString();
                    }
                }
                PushOptions pOpts = null;
                //单个人
                if (push_type == 1)
                {
                    pOpts = new PushOptions(method, apiKey, userid, channelid, device_type, messages, messageksy, unixTime);
                }
                else if (push_type == 2)
                {
                    pOpts = new PushOptions(method, apiKey, tag, device_type, messages, messageksy, unixTime);
                }
                else if (push_type == 3)
                {
                    pOpts = new PushOptions(method, apiKey, device_type, messages, messageksy, unixTime);
                }
                pOpts.message_type = message_type;
                response = Bpush.PushMessage(pOpts);
                Common.LogHelper.WriteLog(response, "百度云推送返回数据");

                var obj = Common.Json.Deserialize<object>(response);
                JsonObject jsonobj = new JsonObject(response);
                string request_id = jsonobj["request_id"].Value;
                if (jsonobj["response_params"] != null) //推送成功
                {
                    response = jsonobj["response_params"]["success_amount"].Value;
                }
                else if (jsonobj["error_msg"] != null) //推送失败
                {
                    response = "推送失败";
                }
            }
            catch (Exception ex)
            {
                response = ex.Message;
                Common.LogHelper.WriteLog(ex.StackTrace, "百度云推送PushMsg错误");
            }
            return response;
        }

        #endregion

        #region 标签接口

        /// <summary>
        /// 标签接口
        /// </summary>
        /// <param name="tag">标签</param>
        /// <param name="user_id">百度云推送分配的用户id</param>
        /// <param name="method">推送接口，set_tag：设置标签，delete_tag：删除标签</param>
        /// <returns></returns>
        public string setTag(string tag, string user_id, string method)
        {
            Common.LogHelper.WriteLog(tag + "  " + user_id + "  " + method, "设置标签");
            string response = "";
            try
            {
                OF.Push.BaiduPush Bpush = new OF.Push.BaiduPush("POST", BaiduSecretKey);
                String apiKey = BaiduApiKey;
                TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
                uint unixTime = (uint)ts.TotalSeconds;

                PushOptions pOpts = null;
                pOpts = new PushOptions(method, apiKey, tag, user_id, unixTime);
                response = Bpush.PushMessage(pOpts);
            }
            catch (Exception ex)
            {
                response = ex.Message;
                Common.LogHelper.WriteLog(ex.StackTrace, "百度云推送setTag错误");
            }
            return response;
        }

        #endregion

    }
}
